package lookups.action;

import java.util.ArrayList;

import lookups.dao.AddressDAO;
import lookups.entity.Address;

import com.opensymphony.xwork2.ActionSupport;

public class PreviousPageAAction extends ActionSupport{
	private String postcode;
	private String street;
	private String town;
	private int pageNum;
	private int PAGE_SIZE=6;
	private int totalPage;
	
	ArrayList<Address> listaddress;
	
	@Override
	public String execute() throws Exception {
		System.out.println("postcode="+postcode);
		System.out.println("street="+street);
		System.out.println("town="+town);
		
		if(pageNum<=0) pageNum=1;
		if(pageNum >1) pageNum--;
		AddressDAO ad=new AddressDAO();
		
		int totalresult=ad.getTotalAddress(postcode, street, town);
		
		totalPage=totalresult/PAGE_SIZE;
		if(totalresult%PAGE_SIZE>0) totalPage++;
		System.out.println("total page="+totalPage);
		
		if(pageNum>=totalPage)pageNum=totalPage;
		
		listaddress=ad.getListAddress(postcode, street, town,pageNum,PAGE_SIZE);
		if(listaddress==null) return ERROR;
		return SUCCESS;
	}

	public String getPostcode() {
		return postcode;
	}

	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getTown() {
		return town;
	}

	public void setTown(String town) {
		this.town = town;
	}

	public ArrayList<Address> getListaddress() {
		return listaddress;
	}

	public void setListaddress(ArrayList<Address> listaddress) {
		this.listaddress = listaddress;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	
	
}
